class Keyword < ActiveRecord::Base
  has_many :indices
  has_one :user
  
  #Meaningless chars are assumed to be cleaned
  CHARS_LIST=["!-/",":-@","[-`","{-~"] 
  
  def before_create
    self.created_at=Time.now
    self.search_times=0
  end
  
  #Puts out search conditions  
  def self.create_query_conditions(query,user,category_id,additional_conditions="")
    searchword_list=Keyword.split_string(query)
    searchword_list.each do |word|
      SearchWord.search_word(word,user)
    end
    return  searchword_list, SplitWord.create_query_conditions(searchword_list,user,category_id,additional_conditions)
  end
  
  def self.create_ajax_conditions(string,category_id,additional_conditions="")
    word_id_list=[]
    SplitWord.split_word(string).each do |word|
      if splitword=SplitWord.find_by_word(word)
        word_id_list=word_id_list<<splitword.id
      end
    end
    return Index.create_query_conditions(word_id_list,category_id,additional_conditions)
  end
  
  def self.split_string(string)
    string.empty? ? [] : Keyword.clear_chars(string.split,CHARS_LIST)
  end
  
  def self.split_word(string)
    SplitWord.split_word_list(Keyword.split_string(string))
  end

private  
  def self.clear_chars(string_list,chars_list)
    word_list=[]
    string_list.each do |string|
      chars_list.each do |char|
        string=string.delete(char)
      end
      word_list=word_list<<string.downcase.to_s
    end
    return word_list
  end
  
end

#搜索关键词
class SearchWord < Keyword

  def create_query_conditions(category_id,additional_conditions="")
    word_id_list=[]
    SplitWord.split_word(self.word).each do |word|
      if splitword=SplitWord.find_by_word(word)
        word_id_list=word_id_list<<splitword.id
      end
    end
    return Index.create_query_conditions(word_id_list,category_id,additional_conditions)
  end
  
  def self.create_searchword(word,user)
    SearchWord.create(:word=>word,
                      :user_id=>user.id)    
  end
  
  def self.search_word(word,user)
    searchword=SearchWord.find_by_word(word)||SearchWord.create_searchword(word,user)
    searchword.update_attributes(:search_times=>searchword.search_times+1,
                                 :search_at=>Time.now,
                                 :search_ip=>user.login_ip)
  end
end

#标签关键词
class TagWord < Keyword
  def create_query_conditions(category_id,additional_conditions="")
    self.update_attributes(:search_at=>Time.now)
    return Index.create_query_conditions([self.id],category_id,additional_conditions)
  end
end

#二分后的词
class SplitWord < Keyword

  def self.create_query_conditions(word_list,user,category_id,additional_conditions="")
    word_id_list=[]
    SplitWord.split_word_list(word_list).each do |splitword|
      word_id_list=word_id_list<<SplitWord.search_by_word(splitword,user)
    end
    return Index.create_query_conditions(word_id_list,category_id,additional_conditions)
  end

  def self.split_word_list(searchword_list)
    word_list=[]
    searchword_list.each do |word|
      word_list=word_list+SplitWord.split_word(word)
    end
    return word_list
  end
   
  def self.split_word(searchword)
    word_list=[]
    if searchword.empty?
      return word_list
    else
      theword=""
      i=0
      while i<searchword.length
        if searchword[i]>127
          word_list<<theword unless theword==""
          theword=""
          unless searchword[i+3]==nil||searchword[i+3]<128
            word_list<<searchword[i...i+6].to_s
          else
            if i==0 || searchword[i-1]<128
              word_list<<searchword[i...i+3].to_s
            end
          end
          i=i+3
        else
          theword=theword+searchword[i].chr.downcase
          i=i+1
        end
      end
      word_list<<theword unless theword==""
    end
    return word_list
  end
  
private  
  def self.search_by_word(word,user)
    if splitword=SplitWord.find_by_word(word)
      splitword.update_attributes(:search_times=>splitword.search_times+1,
                                  :search_at=>Time.now,
                                  :search_ip=>user.login_ip)
      return splitword.id
    else
      return nil
    end
  end
  
end
